﻿using BLL;
using DBUtility;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace APPMsgPush
{
    /// <summary>
    /// AddApp 的摘要说明
    /// </summary>
    public class AddApp : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string fileName = "";
            //如果有文件
            if (context.Request.Files.Count > 0)
            {
                //取到文件对象
                HttpPostedFile file = context.Request.Files[0];
                //取得文件后缀名（带个点）
                string ext = System.IO.Path.GetExtension(file.FileName);

                //判断上传文件的类型，这里是根据文件头判断的，防止用户上传恶意假图
                if (file.ContentType == "application/x-pkcs12")
                {
                    //给文件取随及名
                    try
                    {
                        Random ran = new Random();
                        fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ran.Next(100, 1000) + ext;
                        //保存文件
                        string path = context.Server.MapPath("~/") + "/Certificate/" + fileName;
                        file.SaveAs(path);
                    }
                    catch (Exception ex)
                    {
                        JsonHelper.Fail("上傳文件失敗！" + ex.Message);
                    }
                }
                else
                {
                    JsonHelper.Fail("上傳文件錯誤！");
                }
            }

            string appName = HttpContext.Current.Request.Params["AppName"];//項目名稱
            if (string.IsNullOrEmpty(appName))
            {
                JsonHelper.Fail("請輸入項目名稱！");
            }

            string certificate = fileName;//app證書
            string passWord = HttpContext.Current.Request.Params["PassWord"];//app密碼
            string senderId = HttpContext.Current.Request.Params["SenderId"];//项目Id
            string authorizationToken = HttpContext.Current.Request.Params["AuthorizationToken"];//授权令牌
            string packageName = HttpContext.Current.Request.Params["PackageName"];//包名

            bool bo = false;
            App model = AppBLL.CurrBll.GetmodelByAppName(out bo, appName); //相同的数据不会添加多次，更具appname来区分
            if (model == null)
            {
                model = new App();
            }
            model.AppName = appName;
            model.Certificate = certificate;
            model.PassWord = passWord;
            model.SenderId = senderId;
            model.AuthorizationToken = authorizationToken;
            model.PackageName = packageName;
            if (bo == true)
            {
                if (AppBLL.CurrBll.Update(model) > 0)
                {
                    JsonHelper.Success();
                }
                else
                {
                    JsonHelper.Fail("修改失敗！");
                }
            }
            else
            {
                if (AppBLL.CurrBll.Add(model) > 0)
                {
                    JsonHelper.Success();
                }
                else
                {
                    JsonHelper.Fail("添加失敗！");
                }
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}